<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2024/12/24 0024
  Time: 11:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="UTF-8">
    <title>制定安全库存配置单</title>
    <link rel="stylesheet" href="/bootstrap/bootstrap-5.3.3-dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="/css/manufactureCss/Set_production_process_items.css" />
    <style type="text/css">

    </style>
</head>
<body>
<div class="panel product">
    <div class="panel-heading hhead">
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <span style="color: blue;">您正在做的业务是：</span>
                <li class="breadcrumb-item"><a href="#">库存管理</a></li>
                <li class="breadcrumb-item"><a href="#">安全库存配置管理</a></li>
                <li class="breadcrumb-item active" aria-current="page">制定安全库存配置单</li>
            </ol>
        </nav>
        <div>
            <p><small>符合条件的产品总数：<span style="color: red;"></span>例</small></p>
        </div>
    </div>
    <form id="searchForm">
        <input type="hidden" id="page" name="pageNum" value="1">
        <input type="hidden" id="pageSize" name="pageSize" value="5">
        <input type="hidden" name="checkTag" value="1">
<%--        <input type="hidden" name="designModuleTag" value="0">--%>
        <input type="hidden" name="designCellTag" value="0">
        <input type="hidden" name="firstId" value="">
        <input type="hidden" name="secondId" value="">
        <input type="hidden" name="thirdId" value="">
        <input type="hidden" name="startDate" value="">
        <input type="hidden" name="endDate" value="">
    </form>
    <div class="panel-body bbody">
        <table id="tab" >
            <thead>
            <tr style="background-color: #e8e8e8;">
                <td>产品编号</td>
                <td>产品名称</td>
                <td>用途类型</td>
                <td>档次级别</td>
                <td>I级分类</td>
                <td>II级分类</td>
                <td>III级分类</td>
                <td>制定</td>
            </tr>
            </thead>
            <tbody id="template">

            </tbody>
        </table>
    </div>

    <div class="panel-footer ffooter">
        <div id="footerDiv" class="container-fluid">
            <div class="row">
                <label style="font-weight: 700;" class="col-sm-10">总数：<span id="footNum1" style="color: red;"></span>例&nbsp;&nbsp;&nbsp;&nbsp;
                    当前第<span style="color: red;" id="pageNum1"></span>页&nbsp;&nbsp;&nbsp;&nbsp;
                    共<span style="color: red;" id="pagesNum1"></span>页&nbsp;&nbsp;&nbsp;&nbsp;
                </label>
                <nav aria-label="Page navigation example" class="col-sm-2">
                    <ul class="pagination">
                        <li class="page-item">
                            <a class="page-link" href="#" aria-label="Previous" id="a0">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <li class="page-item"><a class="page-link" href="#" id="a1">1</a></li>
                        <li class="page-item"><a class="page-link" href="#" id="a2">2</a></li>
                        <li class="page-item"><a class="page-link" href="#" id="a3">3</a></li>
                        <li class="page-item">
                            <a class="page-link" href="#" aria-label="Next" id="a4">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
        <div class="modal fade" id="moduleDetailsModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="staticBackdropLabel" aria-hidden="true">
            <div class="modal-dialog modal-fullscreen">
                <div class="modal-content">
                    <div class="modal-header" style="height: 8%">
                        <h3 class="modal-title" id="staticBackdropLabel">制定安全库存配置单</h3>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body" style="height: 100%;width: 100%;margin: -1%">

                        <iframe id="iframe2" src="" style="width: 100%;height: 99%"></iframe>
                    </div>
                    <div class="modal-footer" style="height: 8%">

                        <span>注意：点击提交按钮后再确认方可提交</span>&emsp;&emsp;
                        <button id="btnConcel" class="btn btn-success" data-bs-dismiss="modal">
                            确认<span class="glyphicon glyphicon-remove"></span>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript" src="/js/jquery-3.7.0.min.js"></script>
<script type="text/javascript" src="/bootstrap/bootstrap-5.3.3-dist/js/bootstrap.bundle.min.js"></script>
<script type="text/javascript" src="/js/common.js"></script>
<script type="text/javascript">
    $(function (){
        // 初始化sessionStorage
        getSessionStorage();
        // 初始化表格
        initialArchiveTable(1);

        // 取消按钮
        $("#btnConcel").click(function () {
            $("#moduleDetailsModal").modal("hide");
        })

        $("#moduleDetailsModal").on("hidden.bs.modal", function () {
            if (sessionStorage.length > 0) {
                let data = sessionStorage.getItem("data")
                console.log(data)
                // 发送请求到后端
                $.ajax({
                    url: "/s-cell/insertScell",
                    type: "POST",
                    data: data,
                    contentType: "application/json",
                    dataType: "json",
                    success: function (flag) {
                        if (flag.errCode == "1") {
                            alert(flag.errMsg)
                        } else {
                            if(flag){
                                alert("提交成功，需要复核")
                                initialArchiveTable(1);
                            }else{
                                alert("系统错误")
                            }
                        }
                    },
                    error: function (e) {
                    }
                })
                // 隐藏模态框时，清空sessionStorage
                sessionStorage.clear();
            }
        });

    })

    function initialArchiveTable(pageNum) {
        $("#page").val(pageNum)
        let json = formDateToJson("searchForm");
        $.ajax({
            url: "/d-file/initialArchiveTable",
            type: "POST",
            data: json,
            contentType: "application/json",
            dataType: "json",
            success: function (Page) {
                console.log(Page)
                $("#num").html(Page.total);
                $("#footNum1").html(Page.total);
                $("#pageNum1").html(pageNum);
                let pages = Math.ceil(Page.total / 5);
                $("#pagesNum1").html(pages);
                params = {
                    "First": 1,
                    "Previous": Page.current > 1 ? Page.current - 1 : 1,
                    "Next": Page.current < pages ? Page.current + 1 : pages,
                    "Last": pages
                };
                console.log(params);

                $("#a0").attr("href", "javascript:initialArchiveTable(" + params.First + ")")
                if(pageNum <= 2){
                    a1 = 1;
                    a2 = 2;
                    a3 = 3;
                }else if(pageNum >= pages - 1){
                    a1 = pages - 2;
                    a2 = pages - 1;
                    a3 = pages;
                }else {
                    a1 = pageNum - 1;
                    a2 = pageNum;
                    a3 = pageNum + 1;
                }
                $("#a1").attr("href", "javascript:initialArchiveTable(" + a1 + ")")
                $("#a1").html(a1)
                a1 == pageNum? $("#a1").addClass("active") : $("#a1").removeClass("active")
                $("#a2").attr("href", "javascript:initialArchiveTable(" + a2 + ")")
                $("#a2").html(a2)
                a2 == pageNum? $("#a2").addClass("active") : $("#a2").removeClass("active")
                a3 == pageNum? $("#a3").addClass("active") : $("#a3").removeClass("active")
                $("#a3").attr("href", "javascript:initialArchiveTable(" + a3 + ")")
                $("#a3").html(a3)
                $("#a4").attr("href", "javascript:initialArchiveTable(" + params.Last + ")")
                tbody = $("#template");
                tbody.empty();

                var files = Page.list;
                FileArr = ["productId", "productName","type","productClass", "firstKindName", "secondKindName", "thirdKindName","formulate"]
                for (var i = 0; i < files.length; i++) {
                    var tr = document.createElement("tr");
                    var product = files[i];
                    for (var k in FileArr) {
                        var td = document.createElement("td");
                        console.log(product[FileArr[k]])
                        if (FileArr[k] == "type") {
                            td.innerHTML = product[FileArr[k]] != undefined ? product.dproductUse.useName : "";
                            tr.appendChild(td);
                        }else if(FileArr[k] == "productClass") {
                            td.innerHTML = product[FileArr[k]] != undefined ? product.dgrade.gradeName: "";
                            tr.appendChild(td);
                        }else if(FileArr[k] == "formulate"){
                            let b = "<a href='javascript:designFormulate(" + files[i]["id"] + ", \"" + files[i]["productId"] + "\")' class='btn btn-outline-success ' >制定</a>";
                            td.innerHTML = b;
                            tr.appendChild(td);
                        }else {
                            td.innerHTML = product[FileArr[k]] != undefined ? product[FileArr[k]] : "";
                            tr.appendChild(td);
                        }
                    }
                    tbody[0].appendChild(tr);
                }
                sessionStorage.clear();
            },
            error: function (e) {

            }
        })
    }
    function getSessionStorage(){
        $('input[name="firstId"]').val(sessionStorage.getItem('firstId'))
        $('input[name="secondId"]').val(sessionStorage.getItem('secondId'))
        $('input[name="thirdId"]').val(sessionStorage.getItem('thirdId'))
        $('input[name="startDate"]').val(sessionStorage.getItem('startDate'))
        $('input[name="endDate"]').val(sessionStorage.getItem('endDate'))
    }
    function designFormulate(id,productId){
        // window.location.href = "/d-file/FormulateMaterialCompositionDesign?id=" + id
        $.ajax({
            url: "/s-cell/selectFormulate?productId="+productId,
            type: "get",
            dataType: "json",
            success: function (Page) {
                console.log(Page);
                if (Page){
                    $("#moduleDetailsModal").modal("show");
                    // 存储模块id到sessionStorage
                    sessionStorage.setItem("moduleId", id);
                    $("#iframe2").attr("src", "/s-cell/safety_stock");
                }else{
                    alert("该产品还未分配库房，请先分配库房");
                }
            }
        })
    }


</script>
</body>
</html>
